<?php

/*
 * 汽车之家导购
 * 检查表
 *      xd_xxx,
 *      xd_xxx_data
 *      xx/201307/31/
 *      xxx/SpiderResult.db3
 * @package    system
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: af3e2d6ebca3be71dd9cd00e7e012aa7b0cf62a9 $
 */
set_time_limit(0);
include '../../common.inc.php';
$hd = new ImportLoop();
$hd->setAim('mysql:host=192.168.1.19;dbname=ixdcw_v12', 'root', 'xddlxdcw');
$hd->setSource('E:/SpiderResult.db3', '', '');
$hd->setSourceInfo('content', 'id', ' 已发=0');
$params = array(
	'maxid'     => isset($_GET['maxid']) ? $_GET['maxid'] : 0,
	'id'        => isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : 0,
	'startTime' => isset($_GET['startTime']) ? $_GET['startTime'] : time(),
	'limit'     => 1,
	'interval'  => 4,
	'debug'     => '',
	'moduleid'  => 23, // 资讯 26 导购23
	'catid'     => 0, // 3592 试驾体验   3593 新车图解    3594   购车手册    3595  车型对比
	'picAdd'    => 'http://www.autohome.com.cn/',
	'siteName'  => '汽车之家导购',
	'userid'    => '1',
	'username'  => 'xddlmaster', // id
	'timeType'  => 4, // 1 2013-07-17 10:07:56  2 2013-07-17    3 2013年07月07日
	'dtime'     =>time()-86400,
);

$fields = array(
	'time'     => 'time', // 时间字段
	'title'    => 'title', // 标题字段
	'id'       => 'ID', // id
	'copyfrom' => 'comefrom', // from
);
$hd->initParams($params);

$sql = 'select * from ' . $hd->getSourceTable() . ' where id>' . $hd->getParam('id') . ' and 已发=0 order by id ASC limit ' . $hd->getParam('limit');

$result = $hd->getDbSource()->query($sql);
$rs     = $result->fetchAll();

if (!$rs) {
	$hd->times();
	$hd->end();
} else {
	foreach ($rs as $k => $v) {
		$insert = array();
		$hd->setParam('id', $v[$fields['id']]); // set id

		// set handler over
		$sqlUpdate = Sql::update($hd->getSourceTable(), '已发 =1', "id={$v[$fields['id']]}");
		$title     = empty($v['title']) ? $v['title2'] : $v['title'];
		$time      = !empty($v['time']) ? $v['time'] : $v['time2'];

		if (in_array($v['type'], array('其他媒体转载'))) {
			$hd->getDbSource()->query($sqlUpdate);
			$hd->times("当前处理ID:{$v[$fields['id']]}, 跳过:其他媒体转载<br>", 'warn');
			continue;
		}
		$ids = AutoImport::match($title . $v['pinpai'].$v['chexing']);

		$time = AutoImport::getTime($time, $hd->getParam('timeType'));

		if ($hd->getParam('debug') == 'xddl') {
			$time = time() + 86400 * 10;
		}

		// filter time
		if ($time <= $hd->getParam('dtime')) {
			$hd->getDbSource()->query($sqlUpdate);
			$hd->times("当前处理ID:{$v[$fields['id']]}, 跳过:不匹配的时间<br>", 'warn');
			continue;
		}

		if (!$v['carname']) {
			$hd->getDbSource()->query($sqlUpdate);
			$hd->times("当前处理ID:{$v[$fields['id']]}, 跳过:空车型<br>", 'warn');
			continue;
		}

		if ($v['daohang']) {
			$content = $v['content-1'];
			$content .= trim($v['content-2']) == '[pagebreak]' ? '' : $v['content-2'];
			$content .= trim($v['content-3']) == '[pagebreak]' ? '' : $v['content-3'];
			$content .= trim($v['content-4']) == '[pagebreak]' ? '' : $v['content-4'];
			$content .= trim($v['content-5']) == '[pagebreak]' ? '' : $v['content-5'];
			$content .= trim($v['content-6']) == '[pagebreak]' ? '' : $v['content-6'];
			$content .= trim($v['content-7']) == '[pagebreak]' ? '' : $v['content-7'];
			$content .= trim($v['content-8']) == '[pagebreak]' ? '' : $v['content-8'];
			$content .= trim($v['content-9']) == '[pagebreak]' ? '' : $v['content-9'];
			$content .= trim($v['content-10']) == '[pagebreak]' ? '' : $v['content-10'];
			$content .= trim($v['content-11']) == '[pagebreak]' ? '' : $v['content-11'];
			$content .= trim($v['content-12']) == '[pagebreak]' ? '' : $v['content-12'];
			$content .= trim($v['content-13']) == '[pagebreak]' ? '' : $v['content-13'];
			$content .= trim($v['content-14']) == '[pagebreak]' ? '' : $v['content-14'];
			$content .= trim($v['content-15']) == '[pagebreak]' ? '' : $v['content-15'];
		}
		if (empty($content)) {
			$content = $v['content'];
		}
		$content = preg_replace("/\[pagebreak\]\s+$/", '', $content);

		if (empty($content)) {
			$hd->getDbSource()->query($sqlUpdate);
			$hd->times("当前处理ID:{$v[$fields['id']]}, 跳过:空内容<br>", 'warn');
			continue;
		}

		$content            = Post::clearLink($content);
		$content            = AutoImport::fullUrl($content, 'jpg|jpeg|gif|png|bmp', $hd->getParam('picAdd'));
		$thumb              = AutoImport::saveThumb($content, 1, 150, 113, $hd->getParam('moduleid'), $hd->getParam('userid'), $time, $hd->getParam('picAdd'));
		$insert['title']    = $title;
		$insert['subtitle'] = $v['daodu'];
		$insert['arbid']    = $ids[0];
		$insert['catid']    = getParamCatid($v['type']);
		$hd->setParam('catid', $insert['catid']);
		$insert['armid']     = $ids[1];
		$insert['arcid']     = $ids[2];
		$insert['introduce'] = Str::addSlashes(Str::cut(strip_tags($content), 120));
		$insert['copyfrom']  = $v['comefrom'] ? $v['comefrom'] : $v['comefrom2'];
		$insert['fromurl']   = '';
		$insert['hits']      = rand(20, 5000);
		$insert['thumb']     = $thumb;
		$insert['username']  = $hd->getParam('username');
		$insert['addtime']   = $time;
		$insert['editor']    = $hd->getParam('username');
		$insert['edittime']  = $time;
		$insert['ip']        = '60.208.115.170';
		$insert['status']    = 2;
		$insert['islink']    = 0;
		if (empty($content)) {
			$hd->times("当前处理ID:{$v[$fields['id']]}, 跳过:空内容", 'warn');
		}

		$table = $hd->getTable();

		$sql = Sql::insert($table, $insert);
		if ($hd->getParam('debug') == 'xddl') {
			$hd->times("当前处理ID:{$v[$fields['id']]}, 发表于:{$v[$fields['time']]}<br>".$sql);
			exit();
		}
		if ($hd->getDbAim()->query($sql)) {
			$insertId                 = $hd->getDbAim()->lastInsertId();
			$insertContent            = array();
			$content                  = AutoImport::saveRemote($content, 'jpg|jpeg|gif|png|bmp', $hd->getParam('moduleid'), $hd->getParam('userid'), $time, $hd->getParam('picAdd'));
			$insertContent['itemid']  = $insertId;
			$insertContent['content'] = Str::addSlashes($content);
			$sql                      = Sql::insert($table . '_data', $insertContent);
			if ($hd->getDbAim()->query($sql)) {
				if (!$hd->getParam('debug') == 'xddl') {
					$hd->getDbSource()->query($sqlUpdate);
				}
				$hd->times("当前处理ID:{$v[$fields['id']]}, 发表于:{$v[$fields['time']]}");
			} else {
				$hd->times("当前处理ID:{$v[$fields['id']]}, 错误:", 'error');
				exit($sql);
			}
		} else {
			$hd->times("当前处理ID:{$v[$fields['id']]}, 错误:", 'error');
			exit($sql);
		}
	}
}
echo $hd->start($hd->getParams());
function getParamCatid($catname) {
	switch ($catname) {
		case '新车点评':
		case '选车心得':
		case '购车手册':
			return 3594;
			break;
		case '深度调查':
		case '新车图解':
			return 3592;
			break;
		case '实拍图秀':
			return 3593;
			break;
		case '车型PK':
		case '初步海选':
			return 3595;
			break;
	}
	return 0;
}